<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>PWM example</title>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="alias.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="alias.js"></script>

<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>


<script type="text/javascript">
  jQuery(document).ready(function () {
    if(gref){ // Number all _img and _table classes
      gref();
    }
  });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nRF51 General Code Examples
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('group__pwm__example.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#define-members">Defines</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a>  </div>
  <div class="headertitle">
<div class="title">PWM example</div>  </div>
<div class="ingroups"><a class="el" href="group__nrf__examples__nrf6310.html">nRF6310 example projects</a></div></div>
<div class="contents">

<p>Example of basic PWM generation using freerunning timer. The GPIO pin could for example drive a small speaker. Setup: Standard setup except LED 0 should be connected to pin 18 (port 2, pin 2)  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr><td colspan="2"><h2><a name="define-members"></a>
Defines</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#gaf1aeb7e936e859c23ed991a5710cb38f">PWM_OUTPUT_PIN_NUMBER</a>&#160;&#160;&#160;(18UL)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga8a1b2539cc4b0524d18213da9b73deea">MAX_SAMPLE_LEVELS</a>&#160;&#160;&#160;(256UL)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga10d70890ad1225e866232330b749ecae">TIMER_PRESCALERS</a>&#160;&#160;&#160;4U</td></tr>
<tr><td colspan="2"><h2><a name="func-members"></a>
Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static __INLINE uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga31af9c2e9f3e2c61380411894b5cb173">next_sample_get</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga12c8b827e5eab9d3f93d56f858e735a3">TIMER2_IRQHandler</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga9459bbd9d6c1f423f968799c09f96434">timer2_init</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#gad22626704ea5d45f863163500e355cc3">gpiote_init</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#gaad16890c50156256e260b5205571ddc4">ppi_init</a> (void)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga840291bc02cba5474a4cb46a9b9566fe">main</a> (void)</td></tr>
<tr><td colspan="2"><h2><a name="var-members"></a>
Variables</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#ga2141c8109b25266ef9bb75772414f08f">last_cc0_sample</a></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">static uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pwm__example.html#gaf3984b17a772f2832fcb360ff9f5341e">last_cc2_sample</a></td></tr>
</table>
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<p>Example of basic PWM generation using freerunning timer. The GPIO pin could for example drive a small speaker. Setup: Standard setup except LED 0 should be connected to pin 18 (port 2, pin 2) </p>
<p>Behaviour: This example generates variable duty cycle pulse train on selected GPIO pin. The duty cycle is read as a byte taken from Pin 0-7 and will therefore vary between 1-255. Notice that the value 0 is omitted, this is because of a problem with having two events trigger at the same time only causing one toggle of the output pin. Timer 2 is used to dispatch GPIOTE Toggle events on every compare match. CC1 will set the output pin high while CC0 and CC2 will toggle it low every other turn ending a duty cycle. These compare timings will be set by an interrupt happening every CC1 at the 0(or 256) mark for the period. This interrupt will increase CC1 with one more period and every turn change between setting CC0 and CC2 to the value retrieved from Pin 0-7.</p>
<p>Expected Results: The program will emit light on LED 0 corresponding to what value is pressed in with Buttons 0-7, where Button 0 is bit 0 and Button 7 is bit 7 of a byte. With a higher value the LED will emit brighter light. </p>
<hr/><h2>Define Documentation</h2>
<a class="anchor" id="gaf1aeb7e936e859c23ed991a5710cb38f"></a><!-- doxytag: member="main.c::PWM_OUTPUT_PIN_NUMBER" ref="gaf1aeb7e936e859c23ed991a5710cb38f" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">#define PWM_OUTPUT_PIN_NUMBER&#160;&#160;&#160;(18UL)</td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Pin number for PWM output </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00048">48</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga8a1b2539cc4b0524d18213da9b73deea"></a><!-- doxytag: member="main.c::MAX_SAMPLE_LEVELS" ref="ga8a1b2539cc4b0524d18213da9b73deea" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">#define MAX_SAMPLE_LEVELS&#160;&#160;&#160;(256UL)</td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Maximum number of sample levels </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00050">50</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga10d70890ad1225e866232330b749ecae"></a><!-- doxytag: member="main.c::TIMER_PRESCALERS" ref="ga10d70890ad1225e866232330b749ecae" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">#define TIMER_PRESCALERS&#160;&#160;&#160;4U</td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Prescaler setting for timer </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00051">51</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<hr/><h2>Function Documentation</h2>
<a class="anchor" id="ga31af9c2e9f3e2c61380411894b5cb173"></a><!-- doxytag: member="main.c::next_sample_get" ref="ga31af9c2e9f3e2c61380411894b5cb173" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">static __INLINE uint32_t next_sample_get </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Get next sample. </p>
<dl class="return"><dt><b>Returns:</b></dt><dd>sample_value Next sample value. </dd></dl>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00059">59</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga12c8b827e5eab9d3f93d56f858e735a3"></a><!-- doxytag: member="main.c::TIMER2_IRQHandler" ref="ga12c8b827e5eab9d3f93d56f858e735a3" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void TIMER2_IRQHandler </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Timer 2 peripheral interrupt handler. </p>
<p>&lt; Variable to keep track which CC register is to be used </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00075">75</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga9459bbd9d6c1f423f968799c09f96434"></a><!-- doxytag: member="main.c::timer2_init" ref="ga9459bbd9d6c1f423f968799c09f96434" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">static void timer2_init </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Initialises Timer 2 peripheral. </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00107">107</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="gad22626704ea5d45f863163500e355cc3"></a><!-- doxytag: member="main.c::gpiote_init" ref="gad22626704ea5d45f863163500e355cc3" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">static void gpiote_init </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Initialises GPIO Tasks/Events peripheral. </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00147">147</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="gaad16890c50156256e260b5205571ddc4"></a><!-- doxytag: member="main.c::ppi_init" ref="gaad16890c50156256e260b5205571ddc4" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">static void ppi_init </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>Initialises Programmable Peripheral Interconnect peripheral. </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00170">170</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga840291bc02cba5474a4cb46a9b9566fe"></a><!-- doxytag: member="main.c::main" ref="ga840291bc02cba5474a4cb46a9b9566fe" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int main </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p><a class="el" href="group__gpio__example.html#ga840291bc02cba5474a4cb46a9b9566fe">main()</a> function </p>
<dl class="return"><dt><b>Returns:</b></dt><dd>0. int return type required by ANSI/ISO standard. </dd></dl>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00203">203</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<hr/><h2>Variable Documentation</h2>
<a class="anchor" id="ga2141c8109b25266ef9bb75772414f08f"></a><!-- doxytag: member="main.c::last_cc0_sample" ref="ga2141c8109b25266ef9bb75772414f08f" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">uint32_t <a class="el" href="group__pwm__example.html#ga2141c8109b25266ef9bb75772414f08f">last_cc0_sample</a><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>CC0 register value in the previous round </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00053">53</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
<a class="anchor" id="gaf3984b17a772f2832fcb360ff9f5341e"></a><!-- doxytag: member="main.c::last_cc2_sample" ref="gaf3984b17a772f2832fcb360ff9f5341e" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">uint32_t <a class="el" href="group__pwm__example.html#gaf3984b17a772f2832fcb360ff9f5341e">last_cc2_sample</a><code> [static]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">
<p>CC2 register value in the previous round </p>

<p>Definition at line <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html#l00054">54</a> of file <a class="el" href="nrf6310_2pwm__example_2main_8c_source.html">main.c</a>.</p>

</div>
</div>
</div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
      Copyright &copy 2006-2011 <a href="http://www.nordicsemi.no" style="text-decoration:none">Nordic Semiconductor</a>.
      All Rights Reserved.
      <a href="disclaimer.html">Disclaimer</a>
    </li>
   </ul>
 </div>


</body>
</html>
